namespace ResumableDownload.View.Converters
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using WP7Contrib.Services.DownloadManager;

    public sealed class RequestSpeedToStringConverter : IValueConverter
    {
        private const int KiloByte = 1024;
        
        public static NumberFormatInfo NumberFormatInfo;

        public readonly string SpeedFormat = "Rate: {0} Kb/sec";

        static RequestSpeedToStringConverter()
        {
            NumberFormatInfo = (NumberFormatInfo)CultureInfo.CurrentUICulture.NumberFormat.Clone();
            NumberFormatInfo.NumberDecimalDigits = 0;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var request = value as FileRequest;

            if (request == null || value == DependencyProperty.UnsetValue)
            {
                return DependencyProperty.UnsetValue;
            }
            
            return string.Format(SpeedFormat, request.DownloadRate.ToString("N", NumberFormatInfo));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }
}